﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace HotelProject
{
    public class HotelSearch
    {
        

        
        /// <summary>
        /// This method creates a list of all the hotel rooms.  It then adds any elements that don't match any criteria inputed by the user
        /// to a list of elements to remove.  The elements are then removed from the list of all hotel rooms afterwards
        /// </summary>
        /// <param name="query">the passed in query</param>
        /// <returns>an array of hotel rooms based on the user's criteria</returns>
        public static hotelRoom[] search(HotelSearchQuery query)
        {
            MySQLDatabaseConnector dataBase = new MySQLDatabaseConnector();
           
            if (query == null || query.checkInDate == null || query.checkOutDate == null)
            {
                throw new Exception();
            }

            List<hotelRoom> allHotelRooms = dataBase.getAvailableHotelRoomsByDate(query.checkInDate, query.checkOutDate).ToList<hotelRoom>();
            List<hotelRoom> roomsToRemove = new List<hotelRoom>();

           if (query.roomID != 0)
            {
                foreach (hotelRoom h in allHotelRooms)
                {
                    if (h.roomID != query.roomID)
                    {
                        roomsToRemove.Add(h);
                    }
                }
            }

            if (query.bedSize != null && query.bedSize != "0" )
            {
                foreach (hotelRoom h in allHotelRooms)
                {
                    if (h.bedSize != query.bedSize)
                    {
                        roomsToRemove.Add(h);
                    }
                }
            }

            if (query.floorNumber != 0)
            {
                foreach (hotelRoom h in allHotelRooms)
                {
                    if (h.floorNumber != query.floorNumber)
                    {
                        roomsToRemove.Add(h);
                    }
                }
            }

            if (query.hotelID != 0)
            {
                foreach (hotelRoom h in allHotelRooms)
                {
                    if (h.hotelID != query.hotelID)
                    {
                        roomsToRemove.Add(h);
                    }
                }
            }

            if (query.maxPrice != 0)
            {
                foreach (hotelRoom h in allHotelRooms)
                {
                    if (h.price > query.maxPrice)
                    {
                        roomsToRemove.Add(h);
                    }
                }
            }

            if (query.minPrice != 0)
            {
                foreach (hotelRoom h in allHotelRooms)
                {
                    if (h.price < query.minPrice)
                    {
                        roomsToRemove.Add(h);
                    }
                }
            }

            if (query.roomNumber != 0)
            {
                foreach (hotelRoom h in allHotelRooms)
                {
                    if (h.roomNumber != query.roomNumber)
                    {
                        roomsToRemove.Add(h);
                    }
                }
            }

            if (query.view != null && query.view != "0")
            {
                foreach (hotelRoom h in allHotelRooms)
                {
                    if (h.view != query.view)
                    {
                        roomsToRemove.Add(h);
                    }
                }
            }

            foreach (hotelRoom h in roomsToRemove)
            {
                allHotelRooms.Remove(h);
            }

            hotelRoom[] queriedRooms = allHotelRooms.ToArray();
            return queriedRooms;
        }


    }
}